package com.app.service.impl;

import cn.hutool.core.date.DateUtil;
import com.app.common.SignInContinuous;
import com.app.mapper.SignInMapper;
import com.app.model.IntegralRecordModel;
import com.app.model.IntegralRule;
import com.app.model.SignIn;
import com.app.model.UserModel;
import com.app.service.IIntegralRuleService;
import com.app.service.ISignInService;
import com.app.service.IntegralRecordService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.vo.SignInVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 签到表 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2023-12-12
 */
@Service
public class SignInServiceImpl extends ServiceImpl<SignInMapper, SignIn> implements ISignInService {
    @Autowired
    private IIntegralRuleService iIntegralRuleService;
    @Autowired
    private ISignInService iSignInService;
    @Autowired
    private UserService userService;
    @Autowired
    private IntegralRecordService integralRecordService;

    @Override
    public Object signIn(Integer userId) throws ParseException {
        //查询签到表(时间倒序)
        QueryWrapper<SignIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("create_time");
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time");
        List<SignIn> signInList = super.baseMapper.selectList(queryWrapper);
        // 获取今天的日期
        String today = DateUtil.today();
        if (signInList.isEmpty()) {
            SignIn signIn = new SignIn();
            signIn.setUserId(Integer.valueOf(String.valueOf(userId)));
            signIn.setCreateTime(LocalDateTime.now());
            iSignInService.save(signIn);
            SignInVo signInVo = getSignInVo(signInList, userId);
            signInVo.setContinuousSignInDay(1);
            return signInVo;
        } else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String formattedDateTime = signInList.get(0).getCreateTime().format(formatter);
            // 如果已经签到，返回false
            if (today.equals(formattedDateTime)) {
                System.out.println("今天已经签过到了");
                return "今天已经签过到了";
            } else {
                //签到记录
                SignIn signIn = new SignIn();
                signIn.setUserId(userId);
                signIn.setCreateTime(LocalDateTime.now());
                iSignInService.save(signIn);
                List<SignIn> signIns = super.baseMapper.selectList(queryWrapper);
                return getSignInVo(signIns, userId);
            }
        }
    }

    private SignInVo getSignInVo(List<SignIn> signInList, Integer userId) throws ParseException {
        //获取连续签到天数
        int continuousSignInDay = SignInContinuous.getContinuousSignInDay(signInList);
        //获取积分规则
        List<IntegralRule> integralRuleList = iIntegralRuleService.list();
        //需要连续签到的天数
        int continueSignDay = integralRuleList.get(0).getContinueSignDay();
        //达到连续签到天数后可获取的积分值
        Integer continueSignIntegral = integralRuleList.get(0).getContinueSignIntegral();
        //普通签到获取的积分值
        Integer signIntegral = integralRuleList.get(0).getSignIntegral();
        //用户积分
        UserModel userModel = userService.getById(userId);
        //积分记录
        IntegralRecordModel integralRecord = new IntegralRecordModel();
        integralRecord.setTypeName("签到");
        integralRecord.setCreateTime(CommonHelp.getNowTimestamp());
        integralRecord.setUserId(userId);
        SignInVo vo = new SignInVo();
        if (continuousSignInDay != 0 && continuousSignInDay % continueSignDay == 0) {
            userModel.setIntegral(userModel.getIntegral() + continueSignIntegral);
            userService.updateById(userModel);
            integralRecord.setName("连续签到" + continueSignDay + "天");
            String amountIntegral = "+" + continueSignIntegral + "分";
            integralRecord.setVoucherAmount(Double.valueOf(continueSignIntegral));
            integralRecord.setVoucherType(1);
            integralRecord.setIntegral(amountIntegral);
            integralRecordService.save(integralRecord);
            vo.setContinuousSignInDay(continueSignDay);
            vo.setIntegral(continueSignIntegral);
            return vo;
        }
        else {
            userModel.setIntegral(userModel.getIntegral() + signIntegral);
            userService.updateById(userModel);
            integralRecord.setName("日常签到");
            String amountIntegral = "+" + signIntegral + "分";
            integralRecord.setIntegral(amountIntegral);
            integralRecord.setVoucherAmount(Double.valueOf(signIntegral));
            integralRecord.setVoucherType(1);
            integralRecordService.save(integralRecord);

            if (continuousSignInDay % continueSignDay != 0) {
                vo.setContinuousSignInDay(continuousSignInDay % continueSignDay);
            } else {
                vo.setContinuousSignInDay(continuousSignInDay);
            }
            vo.setIntegral(signIntegral);
            return vo;
        }
    }
}
